/*============================================================================*/
/*                        Tortoise Team			                              */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %tHAL_DcMotor.c%
* Instance:         RPL_1
* %version:         1 %
* %created_by:      Jose Antonio Leon Jazo %
* %date_created:    Sunay September 23 13:24:00 2012 %
*=============================================================================*/
/* DESCRIPTION : C source template file                                       */
/*============================================================================*/
/* FUNCTION COMMENT : Contains the function defitions of the hardware		  */
/* layer of the dc motor	                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 23/09/2012  |   Declared definitions and    | Antonio Leon     */
/*							   the initialisation of the					  */
/*							   dc motor and setting the						  */
/*							   speed 										  */
/*	1.2 	 | 12/10/2012  |   inserting function to stop					  */	
/*							   the dc motor and also						  */
/*							   changing the value that						  */
/*							   resive the funtion set						  */
/*							   dc motors									  */
/*  2.0      | 25/11/2012  |                               | Andres Torres    */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "MAL_EMIOS.h"
#include "MAL_IOAPI.h"
#include "MAL_PWM.h"
#include "HAL_DcMotor.h"
#include "APP_BackWheels.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE variables */

/* WORD variables */

/** \brief Current left dc motor's speed values 
*/
static T_UWORD uw_dutyChL;

/** \brief Current right dc motor's speed values 
*/
static T_UWORD uw_dutyChR;

/* LONG and STRUCTURE variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/*! \def Dc_Channel
Chanel of the Dc motor.
*/
#define DC1_CHANNEL			6
#define DC2_CHANNEL			7

/* \def Dc_Pin
Pin connector of the Dc motor
*/
#define DC1_PIN				PCR_EMIOS_0_6_1
#define DC2_PIN				PCR_EMIOS_0_7_1

/* \def DC_StartS_Us
Starting speed of the Dc motor
*/
#define STARTS_US			MIN_US//(Min_Us + Max_Us) / 2

/* \def Motor_Period_Us
Period of the motor in us
*/
#define MOTOR_PERIOD_US		20000

/* \def Motors_MCB_Channel
Chanel of the counter
*/
#define MOTORS_MBC_CHANNEL	0		

/* \def Counter_Bus
The bus's counter of the pwm
*/
#define COUNTER_BUS			COUNTER_BUS_BCDE

/* Private functions prototypes */
/* ---------------------------- */


/* Exported functions prototypes */
/* ----------------------------- */
void vfnInit_DcMotor(void);
void vfnStop_DcMotor(T_UWORD uw_dcChannel);
void vfnStart_DcMotor(T_UWORD uw_dcChannel);
void vfnSet_DcMotor(T_UWORD uw_speed, T_UWORD uw_dcChan);
T_UWORD vfnGet_Speed(T_UWORD uw_dcChannel);

/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */

/* Exported functions */

/**************************************************************
 *  Name                 :	vfnInit_DcMotor
 *  Description          :	Initializing Dc motors
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Initialize the dc motor
/////////////////////////////////////////////////////////////////////////
void vfnInit_DcMotor(void)
{	
	IO_SetPinFctn(pin_PB0, PA_GPIO);
	IO_SetPinAsOutput(pin_PB0,1);
	
	IO_SetPinFctn(pin_PB1, PA_GPIO);
	IO_SetPinAsOutput(pin_PB1,1);
	
	IO_ChangeFctnPin(16, 1);
	IO_ChangeFctnPin(17, 1);
	
	vfnSetUpClock();
	
	vfnInitPWMChannel(DC1_CHANNEL, STARTS_US, MOTORS_MBC_CHANNEL, MOTOR_PERIOD_US, COUNTER_BUS, DC1_PIN);
	vfnInitPWMChannel(DC2_CHANNEL, STARTS_US, MOTORS_MBC_CHANNEL, MOTOR_PERIOD_US, COUNTER_BUS, DC2_PIN);
}

/**************************************************************
 *  Name                 :	vfnGet_Speed
 *  Description          :	Initializing Dc motors
 *  Parameters           :  [Input uw_dcChannel: selecting the motor, 
 							 Output,Input / output]
 *  Return               :	current speed
 *  Critical/explanation :  No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Obtaine the motor's pwm
/// \param uw_dcChannel Chosing the dc motor
///////////////////////////////////////////////////////////////////////// 
T_UWORD vfnGet_Speed(T_UWORD uw_dcChannel)
{	
	if(uw_dcChannel == 1)
		return uw_dutyChR;
	else if(uw_dcChannel == 0)
		return uw_dutyChL;
			
}

/**************************************************************
 *  Name                 :	vfnSet_DcMotor
 *  Description          :	Seting the speed of the Dc motor
 *  Parameters           :  [Input:uw_speed: speed of the motor; uw_dcChannel: Selecting motor
 * 							 Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
 
///////////////////////////////////////////////////////////////////////
/// \brief Set the speed of the dc motor between the minVal and the maxVal
/// \param uw_speed The value of the dc motor
/// \param uw_dcChannel Chosing the dc motor 
/////////////////////////////////////////////////////////////////////////
void vfnSet_DcMotor(T_UWORD uw_speed,T_UWORD uw_dcChannel)
{	 
	if(uw_speed <= minSp)
	{
		uw_speed = minSp;
	}
	else if(uw_speed >= maxSp)
	{
		uw_speed = maxSp;
	}
	
	if(uw_dcChannel == 0)
	{
		 uw_dutyChL = (T_UWORD) (uw_speed - minSp) * (MAX_US/maxSp) + MIN_US;
		 vfnSetDutyCycle(DC1_CHANNEL, uw_dutyChL);
	}
	else if(uw_dcChannel == 1)
	{
		uw_dutyChR = (T_UWORD) (uw_speed - minSp) * (MAX_US/maxSp) + MIN_US;
		vfnSetDutyCycle(DC2_CHANNEL, uw_dutyChR);
	}
	else if(uw_dcChannel == 2)
	{
		uw_dutyChL = (T_UWORD) (uw_speed - minSp) * (MAX_US/maxSp) + MIN_US;
		uw_dutyChR = (T_UWORD) (uw_speed - minSp) * (MAX_US/maxSp) + MIN_US;
		vfnSetDutyCycle(DC1_CHANNEL, uw_dutyChL);
		vfnSetDutyCycle(DC2_CHANNEL, uw_dutyChR);
	}
}
/**************************************************************
 *  Name                 :	vfnStop_DcMotor
 *  Description          :	Turn off the Dc motors
 *  Parameters           :  [Input: uw_dcChannel: Selecting motor
 *							 Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
///////////////////////////////////////////////////////////////////////
/// \brief Disable the dc motor.
/// \param uw_dcChannel Chosing the dc motor 
/////////////////////////////////////////////////////////////////////////
	void vfnStop_DcMotor(T_UWORD uw_dcChannel)
	{
		if(uw_dcChannel == 0)
		{
			IO_PinWrite(pin_PB0, 0);
		}
		else if(uw_dcChannel == 1)
		{
			IO_PinWrite(pin_PB1, 0);
		}
		else if(uw_dcChannel == 2)
		{
			IO_PinWrite(pin_PB0, 0);
			IO_PinWrite(pin_PB1, 0);	
		}
	}

/**************************************************************
 *  Name                 :	vfnStart_DcMotor
 *  Description          :	Turn off the Dc motors
 *  Parameters           :  [Input: uw_dcChannel: Selecting motor
 *							 Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
 
///////////////////////////////////////////////////////////////////////
/// \brief Enable the dc motor
/// \param uw_dcChannel Chosing the dc motor 
/////////////////////////////////////////////////////////////////////////
void vfnStart_DcMotor(T_UWORD uw_dcChannel)
	{
		if(uw_dcChannel == 0)
		{
			IO_PinWrite(pin_PB0, 1);
		}
		else if(uw_dcChannel == 1)
		{
			IO_PinWrite(pin_PB1, 1);
		}
		else if(uw_dcChannel == 2)
		{
			IO_PinWrite(pin_PB0, 1);
			IO_PinWrite(pin_PB1, 1);
		}
	}

/* ------------------ */
